﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace EveruComponent.Parser.BBCode
{
    public class BBBlockList
    {

        private BBTagDefinition _TagPool;
        private List<BBElement> _Elements;


        public BBTagDefinition TagPool
        {
            get 
            { 
                return _TagPool; 
            }
            set
            {
                _TagPool = value;
            }

        }

        
        /// <summary>
        /// Contains the BB Elements
        /// </summary>
        public List<BBElement> Elements
        {
            get
            {
                if (_Elements == null)
                    _Elements = new List<BBElement>();

                return _Elements;
            }
            set
            {
                _Elements = value;
            }
        }


        #region Converter

        /// <summary>
        /// Convert the actual BBList to a list of Inlines. Just work fine with the default Tags.
        /// </summary>
        /// <returns></returns>
        public List<Inline> ToInlineList(HyperlinkHandler HandleHyperlink)
        {
            List<Inline> Inlines = new List<Inline>();


            foreach (BBElement elm in _Elements)
            {
                Inline ActInline = new Run();

                #region Image
                if (elm.HasTag("img"))
                {
                    ActInline = new InlineUIContainer();
                    Image img = new Image();

                    try
                    {
                        Uri url = new Uri(elm.Text);
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = url;
                        bi.EndInit();

                        img.Source = bi;
                        img.Stretch = System.Windows.Media.Stretch.None;
                        ((InlineUIContainer)ActInline).Child = img;
                    }
                    catch { }
                } 
                #endregion
                #region Url
                else if (elm.HasTag("url"))
                {
                    if (HandleHyperlink == null)
                    {
                        ActInline = new Hyperlink();

                        if (elm.HasTag("b"))
                        {
                            ((Hyperlink)ActInline).FontWeight = FontWeights.Bold;
                        }

                        if (elm.HasTag("u"))
                        {
                            ((Hyperlink)ActInline).TextDecorations = TextDecorations.Underline;
                        }

                        if (elm.HasTag("i"))
                        {
                            ((Hyperlink)ActInline).FontStyle = FontStyles.Italic;
                        }

                        IBBTag UriTag = elm.GetTag("url");

                        //Check the type of Tag
                        if (UriTag.GetType() == typeof(BBContentTag))
                        {
                            ((Hyperlink)ActInline).Inlines.Add(new Run(elm.Text));
                            try
                            {
                                ((Hyperlink)ActInline).NavigateUri = new Uri(elm.Text);
                            }
                            catch { }
                        }
                        else if (UriTag.GetType() == typeof(BBPropertyTag))
                        {
                            ((Hyperlink)ActInline).Inlines.Add(new Run(elm.Text));
                            try
                            {
                                ((Hyperlink)ActInline).NavigateUri = new Uri((UriTag as BBPropertyTag).Property);
                            }
                            catch { }
                        }


                        if (elm.HasTag("size"))
                        {
                            int Size;
                            IBBTag SizeTag = elm.GetTag("size");

                            if (int.TryParse(((BBPropertyTag)SizeTag).Property, out Size))
                            {
                                ((Hyperlink)ActInline).FontSize = Size;
                            }
                        }
                    }
                    else
                    {
                        ActInline = HandleHyperlink(elm.Text, elm.Tags);
                    }



                } 
                #endregion
                #region Text
                else
                {
                    ((Run)ActInline).Text = elm.Text;

                    if (elm.HasTag("b"))
                    {
                        ((Run)ActInline).FontWeight = FontWeights.Bold;
                    }

                    if (elm.HasTag("u"))
                    {
                        ((Run)ActInline).TextDecorations = TextDecorations.Underline;
                    }

                    if (elm.HasTag("i"))
                    {
                        ((Run)ActInline).FontStyle = FontStyles.Italic;
                    }

                    if (elm.HasTag("size"))
                    {
                        int Size;
                        IBBTag SizeTag = elm.GetTag("size");

                        if (int.TryParse(((BBPropertyTag)SizeTag).Property, out Size))
                        {
                            ((Run)ActInline).FontSize = Size;
                        }
                    }

                } 
                #endregion

                Inlines.Add(ActInline);

            }


            return Inlines;
        }


        /// <summary>
        /// Convert the actual BBList to a list of Inlines. Just work fine with the default Tags.
        /// </summary>
        /// <returns></returns>
        public List<Inline> ToInlineList()
        {
            return ToInlineList(null);
        }


        #endregion


        #region Delegates

        public delegate Inline HyperlinkHandler(string Text, List<IBBTag> Tags);

        #endregion


    }
}
